package com.shop.dao.impl;

import com.shop.dao.ICartDao;
import com.shop.entity.Cart;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartDaoImpl extends BaseDao implements ICartDao {

    private static Map<String,String> map = new HashMap<>();

    static {
        map.put("c_id","cid");
        map.put("u_id","uid");
        map.put("p_id","pid");
        map.put("c_count","ccount");
        map.put("c_num","cnum");
    }

    /**
     * 通过uid查询所有购物车项
     * @param uid
     * @return
     */
    @Override
    public List<Cart> findCartsByUid(int uid) {
        String sql = "select c_id,u_id,p_id,c_count,c_num from cart where u_id = ?";
        return queryForList(Cart.class,sql,map,uid);
    }

    /**
     * 删除购物车项
     * @param uid
     * @return
     */
    @Override
    public int clear(int uid) {
        String sql = "delete from cart where u_id = ?";
        return update(sql,uid);
    }

    /**
     * 从购物车id删除一个商品
     * @param cid
     * @return
     */
    @Override
    public int delteOne(int cid ) {
        String sql = "delete from cart where c_id=? ";
        return update(sql,cid);
    }

    /**
     * 通过id和要修改的数量来购物车数量
     * @param cid
     * @param cnum
     * @return
     */
    @Override
    public int update(int cid,int cnum,int ccount) {
        String sql="update cart set c_num=?,c_count=? where c_id = ?";
        return update(sql,cnum,ccount,cid);
    }

    @Override
    public int insert(Cart cart) {
        String sql="insert into cart (u_id,p_id,c_count,c_num) values (?,?,?,?)";
        Object[] objects= {cart.getUid(),cart.getPid(),cart.getCcount(),cart.getCnum()};
        return update(sql,cart.getUid(),cart.getPid(),cart.getCcount(),cart.getCnum());
    }

}
